<template>
  <!-- 战败车型 -->
  <div>
    <BreakSpace label="黑名单经销商信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="2"
      labelWidth="100"
      :isSubmitBtn="false"
      :formType="type"
    />

    <br />
    <br />
    <br />
    <BreakSpace label="改派经销商" :containerStyle="{ marginBottom: '10px' }" />
    <ul class="diyNav">
      <li v-for="x in reassignDealerList" :key="x.key">
        <span>{{x}}</span>
        <el-button type="text" @click="ilClose(x)" icon="el-icon-close"></el-button>
      </li>
    </ul>

    <br />
    <br />
    <br />
    <BreakSpace label="经销商搜索并选择" :containerStyle="{ marginBottom: '10px' }" />

    <TopFilter
      :list="topFilterList"
      :cols="2"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      height="auto"
      class="diyTable"
      :columns="columns"
      :fetchapi="fetchApi"
      :isToperInfo="false"
      :params="fetchParams"
      :datakey="'records'"
      :selectionType="selectType"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">添加改派经销商</AjaxButton>
    </div>
  </div>
</template>

<script>
import { queryWhiteModels, getOrgList, addReassign } from '@/api/potenCus/basedata/dealerBlacklist';
import { dictionary } from '@/utils/dictMixin';
import dealerBlacklistModel from './dealerBlacklistModel';
export default {
  name: 'dealerBlacklistModels',
  mixins: [dictionary],
  components: {
    dealerBlacklistModel
  },
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    },
    selectType: {
      type: String,
      default: 'single'
    }
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      formList: this.createFormList(),
      reassignDealerList: [],
      dealerBlacklistId: null,
      dialogTitle: '',
      fetchApi: queryWhiteModels,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { add: '新增', edit: '编辑', show: '显示' },
      selectedRow: null
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '关键字',
          fieldName: 'dealerCode',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '所属事业部',
          fieldName: 'orgId',
          placeholder: '请选择',
          itemList: this.createDictList('1045')
        }
      ];
    },
    createFormList() {
      this.type = 'show';
      let list = [
        {
          type: 'INPUT',
          fieldName: 'dealerCode',
          label: '经销商代码',
          initialValue: this.rowData.dealerCode
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          initialValue: this.rowData.dealerName
        }
      ];
      return list;
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所属事业部',
          dataIndex: 'orgName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `设置${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    del(dealerCode) {
      var index = this.reassignDealerList.findIndex(item => {
        if (item == dealerCode) {
          return true;
        }
      });
      this.reassignDealerList.splice(index, 1);
    },
    ilClose(dealerCode) {
      this.del(dealerCode);
    },
    selectChangeHandle(rows) {
      if (!rows.length) return;

      for (let i in this.reassignDealerList) {
        if (this.reassignDealerList[i] == rows[0].dealerCode) {
          return;
        }
      }
      this.reassignDealerList.push(rows[0].dealerCode);
    },

    closeDialog(param) {
      //取消按钮
      this.$emit('close', false, param);
    },

    //回显问题
    // syncDataHandle(dataList, isFirst) {
    //   let as = this.rowData.reassignDealerCode;
    //   let av = as.split(',');
    //   if (dataList[1]) {
    //     for (let i in dataList) {
    //       //console.log('wewer', dataList[1]);
    //       for (let j in av) {
    //         if (av[j] == dataList[i].dealerCode) {
    //           this.selections.push(dataList[i]);
    //         }
    //       }
    //     }
    //   }
    // },
    async saveDialog(param) {
      //加入黑名单
      // console.log('加入黑名单',this.selectedRow)
      console.log({ dealerList: this.reassignDealerList, dealerBlacklistId: this.dealerBlacklistId });
      const res = await addReassign({ reassigndDalerList: this.reassignDealerList, dealerBlacklistId: this.dealerBlacklistId });
      if (res.resultCode === 200) {
        this.$message.success('添加改派经销商成功!');
        this.closeDialog('refresh');
      }
    },
    async getOrgList() {
      const res = await getOrgList();
      if (res.resultCode === 200) {
        this.topFilterList[1].itemList = res.data.map(x => ({ value: x.orgId, text: x.orgName }));
      }
    }
  },

  mounted() {
    let as = this.rowData.reassignDealerCode;
    if (as) {
      let av = as.split(',');
      this.reassignDealerList = av;
    }
    this.dealerBlacklistId = this.rowData.dealerBlacklistId;
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getOrgList();
  }
};
</script>

<style lang="less" scoped>
.diyNav {
  padding-left: 12px;
  li {
    display: inline-block;
    padding: 0 5px;
    height: 30px;
    background-color: #f1f8ff;
    border: 1px solid #bbdafa;
    border-radius: 4px;
    margin-right: 5px;
    span {
      font-size: 12px;
      height: 30px;
      padding-right: 5px;
    }
  }
}
.diyTable {
  /deep/.spin-nested-loading {
    width: 100%;
    border-left: #ddd solid 1px !important;
    overflow: hidden !important;
    .spin-container {
      margin-left: -51px !important;
      width: calc(100% + 51px) !important;
      .el-table {
        .el-table__fixed {
          display: none !important;
        }
      }
    }
  }
}
</style>
